import { FC } from "react";
import type { FormProps } from "antd";
import { Button, Form, Input, message, Rate, Upload } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import TextArea from "antd/es/input/TextArea";
import { getBaseUrl, getUploadUrl } from "@utils/WeStudyUtil";
import SelectKnowledgeTree from "src/common/component/SelectKnowledgeTree";
import { httpPost } from "@utils/HttpUtil";

type FieldType = {
  title?: string;
  problemImg?: any;
  answer?: string;
  explainText?: string;
  explainImg?: any;
  rate?: any;
  knowledge?: any;
};

const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
  console.log(values);
    if (values.problemImg !== undefined && values.problemImg !== null) {
      var id = values.problemImg[0].response.data;
      values.problemImg = id;
    }
    if (values.explainImg !== undefined && values.explainImg !== null) {
      var id = values.explainImg[0].response.data;
      values.explainImg = id;
    }
    console.log(values);
    //提交题目
    var problemId = httpPost("/problem/submitProblem", {
      type: "blank",
      title: values.title,
      content: values,
    }).data;
    //提交知识题目关联关系
    httpPost("/problemKnowledge/editProblemKnowledge",{
      problemId: problemId,
      KnowledgeList: values.knowledge
    });
    message.success("提交题目成功");
};

const onFinishFailed: FormProps<FieldType>["onFinishFailed"] = (errorInfo) => {
    console.log("Failed:", errorInfo);
    message.error("提交填空题失败" + errorInfo);
};

const normFile = (e:any) =>{
    if(Array.isArray(e)){
        return e;
    }
    return e && e.fileList;
}

const FunctionSubmitBlank: FC = () => {

  const [form] = Form.useForm();

  const handleChildChange = (value: any) => {
    form.setFieldsValue({knowledge: value});
  }

  const uploadUrl = getUploadUrl();

  return (
    <>
      <Form
        name="basic"
        form={form}
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        style={{ maxWidth: 600, marginTop: 40 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item<FieldType> label="题目（标题）" name="title" required>
          <TextArea rows={6} placeholder={"题目文字标题"} maxLength={1000} />
        </Form.Item>

        <Form.Item<FieldType>
          label="题目（图片）"
          name="problemImg"
          required
          // 下面两条必须存在
          valuePropName="fileList"
          getValueFromEvent={normFile}
        >
          <Upload action={uploadUrl} listType="picture" maxCount={1}>
            <Button type="primary" icon={<UploadOutlined />}>
              上传题目截图
            </Button>
          </Upload>
        </Form.Item>

        <Form.Item<FieldType>
          label="答案"
          name="answer"
          required
          rules={[{ required: true, message: "填空题答案请不要含有特殊字符" }]}
        >
          <Input />
        </Form.Item>

        <Form.Item<FieldType>
          label="解析（文字）"
          name="explainText"
          rules={[
            {
              required: false,
              message: "请上传题目解析文字",
            },
          ]}
        >
          <TextArea
            rows={6}
            placeholder={"请上传题目解析文字"}
            maxLength={1000}
          />
        </Form.Item>

        <Form.Item<FieldType>
          label="解析（图片）"
          name="explainImg"
          required
          // 下面两条必须存在
          valuePropName="fileList"
          getValueFromEvent={normFile}
        >
          <Upload action={uploadUrl} listType="picture" maxCount={1}>
            <Button type="primary" icon={<UploadOutlined />}>
              上传解析截图
            </Button>
          </Upload>
        </Form.Item>

        <Form.Item<FieldType>
          label="难度系数"
          name="rate"
          rules={[{ required: true, message: "最低1星，最高5星" }]}
        >
          <Rate />
        </Form.Item>

        <Form.Item<FieldType> label="知识点" name="knowledge" required>
          <SelectKnowledgeTree onChange={handleChildChange} />
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
          <Button type="primary" htmlType="submit">
            提交题目
          </Button>
        </Form.Item>
      </Form>
    </>
  );
};

export default FunctionSubmitBlank;
